<template>
  <div>
    <common-head></common-head>
    <div class="cates-wrap">
      <div class="cates">
        <!-- 多类名语法
        :class = '['类名1','类名2',{类名3：条件表达式}]'
         -->
        <div
        :class='["cate",{active:index === current}]'
        v-for="(lcate, index ) in cates"
        :key = 'lcate.id'
        @click="changeCate(index)"
         >
         {{ lcate.name }}
         </div>
      </div>
      <div class="cate-detail">
         <div
         class="c-items"
         v-for="item in items"
         :key='item.id'
         >
         <div class="item-name">{{ item.name }}</div>
         <van-icon
         name="arrow"
         @click="toDetail(item.id)"
         />
         </div>
      </div>
    </div>
    <my-tabbar></my-tabbar>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import MyTabbar from '@components/MyTabbar'
import { getCates, getCateList } from '@api'
export default {
  name: 'cates',
  data () {
    return {
      cates: [],
      current: 0,
      items: []
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    // 切换分类
    changeCate (index) {
      this.current = index
      this.fetchCateItems()
    },
    // 获取所有分类
    fetchCates () {
      getCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchCateItems()
        }
      })
    },
    // 获取对应分类下面的商品
    fetchCateItems () {
      getCateList({
        categoryId: this.cates[this.current].id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    },
    // 点击进入详情页
    toDetail (id) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    }
  },
  components: {
    MyTabbar,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.cates-wrap{
  position: fixed;
  top: 50px;
  bottom: 50px;
  left: 0;
  right: 0;
  border-top: 2px solid #ccc;
  display: flex;
  .cates{
    flex: 2;//与下边相结合就代表分别占居父标签在主轴方向的比例
    border: 1px solid #ccc;
    .cate{
      line-height: 50px;
      text-align: center;
      font-size: 14px;
      border-bottom: 1px solid #ccc;
      color: #333;
      &.active{
        border-left: 3px solid #884e22;
        color: #884e22;
      }
    }
  }
  .cate-detail{
    flex: 5;
    padding: 15px;
    font-size: 14px;
    .c-items{
      line-height: 50px;
      display: flex;
      justify-content: space-between;
      .van-icon{
        line-height: 50px;
      }
    }
  }
}
</style>
